class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 2));
        for (int i = 0;i < m + 1;i++)
        {
            for (int j = 0;j < n + 2;j++)
            {
                if ((j == 0 || j == n + 1) && i != 0)
                {
                    dp[i][j] = INT_MAX;
                }
            }
        }
        for (int i = 1;i <= m;i++)
        {
            for (int j = 1;j <= n;j++)
            {
                dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i - 1][j + 1]) + matrix[i - 1][j - 1];
            }
        }
        int tmp = INT_MAX;
        for (int j = 1;j <= n;j++)
        {
            tmp = min(tmp, dp[n][j]);
        }
        return tmp;


    }
};